<template>
	<view class="index-page">
		<u-navbar :autoBack="false" :bgColor="navbarBg">
			<view slot="left" class="navbar-title" :style="{ color: navbarTitleColor }">{{ config.web_site_title }}</view>
		</u-navbar>
		
		<view class="container">
			<view class="top-box">
				<image class="icon" src="@/static/icon-1.png" mode="aspectFill"></image>
				<text class="text">{{ config.web_site_title }}</text>
			</view>
			<view class="swiper-box">
				<u-swiper
					:list="swiperList"
					height="430rpx"
					radius="0" keyName="pic"
					circular indicatorStyle="right: 20px"
					@change="swiperChange"
					@click="swiperClick"
				>
					<view slot="indicator" class="indicator-num">
						<view class="left">
							<text>{{ swiperCurrent + 1 }}</text>
						</view>
						<view class="right">
							<text>{{ swiperList.length }}</text>
						</view>
					</view>
				</u-swiper>
			</view>
			<view class="goods-box">
				<image class="bg" :src="config.index_img_2" mode="aspectFill"></image>
				<view class="goods" @click="goUrl('/pages/index/goods')">
					<image class="image" :src="goodsIndex.img_path" mode="aspectFill"></image>
					<view class="content">
						<view class="title u-line-1">{{ goodsIndex.name }}</view>
						<view class="bottom">
							<view class="price">￥{{ goodsIndex.price }}</view>
							<button class="btn" size="mini">我要加入</button>
						</view>
					</view>
				</view>
			</view>
			<block v-if="config.wx_shenhe == '0'">
				<video class="video" :style="{ height: videoHeight + 'rpx' }" :src="config.index_img" @loadedmetadata="loadedmetadata"></video>
			</block>
			<!-- <image class="register" src="@/static/index-2.jpg" mode="aspectFill"></image> -->
		</view>
		
		<u-tabbar :value="0" activeColor="#5749f7" inactiveColo="#333" fixed placeholder safeAreaInsetBottom zIndex="100">
			<u-tabbar-item text="首页" icon="/static/tabbar/index_fill.png"></u-tabbar-item>
			<u-tabbar-item text="推广" icon="/static/tabbar/share.png" @click="tabbarChang('/pages/share/share')"></u-tabbar-item>
			<u-tabbar-item text="我的" icon="/static/tabbar/my.png" @click="tabbarChang('/pages/my/my')"></u-tabbar-item>
		</u-tabbar>
		
		<view class="service-btn" @click="serviceShow = true">
			<image class="icon" src="@/static/icon-2.png" mode="aspectFill"></image>
			<view class="text">客服</view>
		</view>
		
		<u-popup :show="serviceShow" round="28rpx" mode="center" closeable :safeAreaInsetBottom="false" @close="serviceShow = false">
			<view class="service-popup">
				<view class="title">添加导师免费领资料</view>
				<view class="qrcode-box">
					<image class="qrcode" :src="config.kefu_img" mode="aspectFill" show-menu-by-longpress></image>
				</view>
				<view class="tips">长按识别二维码</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { getSwiper, getConfig, getGoodsList, getMaterial } from '@/api/api.js'
	
	export default {
		data () {
			return {
				transparency: 0,
				
				serviceShow: false,
				
				swiperCurrent: 0,
				swiperList: [],
				
				goodsIndex: {},
				
				config: {},
				
				videoHeight: 420,
			}
		},
		onLoad () {
			this.loadData()
		},
		methods: {
			loadData () {
				getConfig().then(res => {
					if (res) {
						this.config = res.data
					}
				})
				
				getSwiper().then(res => {
					if (res) {
						this.swiperList = res.data.banner
					}
				})
				
				getGoodsList({
					page: 1,
					page_size: 20,
					filters: {
						recommend: 1,
						goods_type: 1
					}
				}).then(res => {
					if (res) {
						this.goodsIndex = res.data.data[0]
					}
				})
			},
			goUrl (url) {
				uni.navigateTo({
					url
				})
			},
			swiperChange (e) {
				this.swiperCurrent = e.current
			},
			swiperClick (index) {
				const urlSwitch = this.swiperList[index].url_switch
				if (urlSwitch == 1) {
					const type = this.swiperList[index].url_type
					if (type == 'h5') {
						const url = this.swiperList[index].params.url
						uni.navigateTo({
							url: '/pages/my/webView',
							success: (res) => {
								res.eventChannel.emit('onUrl', { url: url })
							}
						})
					} else if (type == 'pages') {
						uni.navigateTo({
							url: this.swiperList[index].params.url
						})
					} else if (type == 'goods_detail') {
						const spu = this.swiperList[index].params.spu
						uni.navigateTo({
							url: '/pages/index/detail?spu=' + spu
						})
					}
				}
			},
			loadedmetadata (e) {
				let width = e.detail.width
				let height = e.detail.height
				
				let ratio = 750 / width
				
				if (height > 0) {
					this.videoHeight = parseInt(height * ratio)
				}
			},
			tabbarChang (url) {
				uni.switchTab({
					url: url
				})
			}
		},
		computed: {
			navbarBg () {
				return `rgba(255, 255, 255, ${this.transparency})`
			},
			navbarTitleColor () {
				return `rgba(0, 0, 0, ${this.transparency})`
			}
		},
		onPageScroll (e) {
			if (e.scrollTop > 100) {
				this.transparency = 1
			} else {
				this.transparency = 0
			}
		},
	}
</script>

<style lang="scss">
.index-page {
	/* #ifdef H5 */
	padding-bottom: var(--window-bottom);
	/* #endif */
	.navbar-title {
		color: transparent;
	}
	.top-box {
		box-sizing: border-box;
		height: 200rpx;
		padding: 108rpx 26rpx 0;
		background: linear-gradient(-90deg, #180ba5, #4537e0);
		.icon {
			width: 42rpx;
			height: 42rpx;
			vertical-align: middle;
		}
		.text {
			margin-left: 12rpx;
			color: #fff;
			font-size: 40rpx;
			vertical-align: middle;
		}
	}
	.video {
		width: 750rpx;
		height: 420rpx;
		vertical-align: middle;
	}
	.goods-box {
		position: relative;
		.bg {
			width: 750rpx;
			height: 860rpx;
		}
		.goods {
			position: absolute;
			bottom: 16rpx;
			left: 36rpx;
			right: 28rpx;
			z-index: 2;
			padding: 0 22rpx 26rpx 24rpx;
			display: flex;
			.image {
				width: 150rpx;
				height: 150rpx;
				border-radius: 24rpx;
			}
			.content {
				margin-left: 20rpx;
				padding-top: 20rpx;
				flex: 1;
				display: flex;
				flex-direction: column;
				.title {
					color: #fff;
					font-size: 32rpx;
					font-weight: bold;
				}
				.bottom {
					margin-top: auto;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.price {
						color: #9bfb4e;
						font-size: 26rpx;
						font-weight: bold;
					}
					.btn {
						margin: 0;
						padding: 0;
						width: 214rpx;
						height: 76rpx;
						border-radius: 76rpx;
						color: #3f3307;
						font-size: 32rpx;
						font-weight: bold;
						background-color: #ffd849;
					}
				}
			}
		}
	}
	.register {
		width: 750rpx;
		height: 500rpx;
	}
	.service-btn {
		position: fixed;
		bottom: 216rpx;
		right: 16rpx;
		z-index: 10;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		background: linear-gradient(-90deg, #28c367, #17b48d);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		.icon {
			width: 40rpx;
			height: 40rpx;
			animation: scale infinite 1s;
		}
		.text {
			margin-top: 4rpx;
			color: #fff;
			font-size: 20rpx;
		}
	}
	
	@keyframes scale {
		0% {
			transform: scale(1);
		}
		50% {
			transform: scale(1.2);
		}
		100% {
			transform: scale(1);
		}
	}
	
	.service-popup {
		width: 630rpx;
		border-radius: 28rpx;
		padding: 80rpx 0 86rpx;
		background: linear-gradient(0deg, #ad3368, #2751e3);
		color: #fff;
		text-align: center;
		.title {
			font-size: 48rpx;
			font-weight: bold;
		}
		.qrcode-box {
			margin: 40rpx auto 26rpx;
			width: 426rpx;
			height: 426rpx;
			border-radius: 16rpx;
			text-align: center;
			line-height: 426rpx;
			background-color: #fff;
			.qrcode {
				width: 360rpx;
				height: 360rpx;
			}
		}
		.tips {
			font-size: 26rpx;
		}
	}
	
	::v-deep {
		.u-popup .icon--u-icon {
			width: 40rpx;
			height: 40rpx;
			border-radius: 50%;
			border: 1rpx solid rgba(255, 255, 255, 0.8);
			background-color: rgba(255, 255, 255, 0.2);
			display: flex !important;
			justify-content: center;
			.u-icon__icon {
				font-size: 20rpx !important;
				color: rgba(255, 255, 255, 0.8) !important;
			}
		}
	}
}
</style>
